﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Web;

namespace LuKou.Web.UserAuth
{
    /// <summary>
    /// 网站权限表
    /// </summary>
    [Serializable]
    public class Authority : IAuthority
    {
        /// <summary>
        /// 访客
        /// </summary>
        public bool Guest { get; set; }

        #region 用户权限

        /// <summary>
        /// 用户登录成功
        /// </summary>
        public bool IsUserLogined { get; set; }

        #endregion

        #region 商户权限

        /// <summary>
        /// 是否商户
        /// </summary>
        public bool IsSeller { get; set; }

        /// <summary>
        /// 是否VIP会员
        /// </summary>
        public bool IsVipSeller { get; set; }

        #endregion

        #region 系统管理权限
        /// <summary>
        /// 后台权限
        /// </summary>
        public bool AdminLink { get; set; }
        /// <summary>
        /// 设置系统
        /// </summary>
        public bool Config { get; set; }
        #endregion

        public Authority()
        {
            this.Guest = true;
            this.IsSeller = false;
            this.IsVipSeller = false;
            this.AdminLink = false;
            this.Config = false;
            this.IsUserLogined = false;
        }

        #region 权限操作
        private static PropertyInfo[] properties = null;
        protected virtual PropertyInfo[] GetProperties()
        {
            if (properties == null)
                properties = this.GetType().GetProperties();
            return properties;
        }

        /// <summary>
        /// 权限转代码
        /// </summary>
        /// <returns></returns>
        public virtual string ToCode()
        {
            StringBuilder sb = new StringBuilder();
            foreach (PropertyInfo pi in GetProperties())
            {
                sb.AppendFormat("{0}={1}|", pi.Name, pi.GetValue(this, null));
            }
            sb.Remove(sb.Length - 1, 1);
            return sb.ToString();
        }

        public virtual void Fill(string code)
        {
            string[] ss = code.Split('|');
            foreach (string s in ss)
            {
                string[] tags = s.Split('=');
                if (tags.Length != 2)
                    throw new Exception("错误的Code信息");
                PropertyInfo p = GetProperties().Single(gp => gp.Name == tags[0]);
                p.SetValue(this, Convert.ChangeType(tags[1], p.PropertyType), null);
            }
        }

        public virtual void FillWithWebRequest(System.Web.HttpRequestBase request)
        {
            PropertyInfo[] pis = GetProperties();
            foreach (PropertyInfo p in pis)
            {
                string temp = request.Form[p.Name];
                if (!string.IsNullOrEmpty(temp))
                {
                    p.SetValue(this, Convert.ChangeType(temp, p.PropertyType), null);
                }
                else
                {
                    p.SetValue(this, false, null);
                }
            }
        }
        #endregion
    }
}
